package com.android.clinic.doctors.app.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.clinic.doctors.app.R;
import com.android.clinic.doctors.app.utils.Toasty;
import com.kennyc.view.MultiStateView;

/**
 * Created by cheng on 2018/6/11.
 */
public abstract class BaseFragment extends Fragment {

    protected Activity mActivity;
    protected MultiStateView mMultiStateView;
    protected View bindView;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mActivity = (Activity) context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_base, null);
        bindView = getActivity().getLayoutInflater().inflate(getLayoutResId(), container, false);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        bindView.setLayoutParams(params);

        mMultiStateView = rootView.findViewById(R.id.fragment_base_main);
        LinearLayout convertView = rootView.findViewById(R.id.fragment_base_content);
        convertView.addView(bindView);
        return rootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(savedInstanceState);
        loadData();
    }

    protected void setMultiStateView(int state) {
        if (null != mMultiStateView) {
            mMultiStateView.setViewState(state);
        }
    }

    protected void setMultiStateView(int state, String empty) {
        if (null != mMultiStateView) {
            mMultiStateView.setViewState(state);
            ((TextView) mMultiStateView.getView(state).findViewById(R.id.tv_empty)).setText(empty);
        }
    }

    public void setMultiStateViewListener(View.OnClickListener onClickListener){
        if (mMultiStateView != null && mMultiStateView.getView(MultiStateView.VIEW_STATE_ERROR) != null) {
            mMultiStateView.getView(MultiStateView.VIEW_STATE_ERROR).setOnClickListener(onClickListener);
        }
    }

    protected void showToast(String msg) {
        if (TextUtils.isEmpty(msg))
            return;
        Toasty.showShort(mActivity, msg);
    }

    protected void startToActivity(Class<?> cls) {
        startActivity(new Intent(mActivity, cls));
    }

    protected void startToActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent(mActivity, cls);
        intent.putExtras(bundle);
        startActivity(intent);
    }


    protected <T extends View> T findViewById(int viewId) {
        return bindView.findViewById(viewId);
    }

    protected abstract int getLayoutResId();

    protected abstract void initView(@Nullable Bundle savedInstanceState);

    protected void loadData(){}
}
